/**
 * @file poj/2342/main.cpp
 * @brief
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/6/22 10:09:05
 **/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 6000 + 4;
vector<int> adj[N];
int n, a[N], f[N][2];
int dp(int u, int s) {
  int &d = f[u][s];
  if (d > 0) return d;
  if (s) d = a[u];
  for (int v : adj[u]) d += s ? dp(v, 0) : max(dp(v, 1), dp(v, 0));
  return d;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  int rt = 0;
  for (int u, v; cin >> v >> u && u;) {
    adj[u].push_back(v);
    if (!rt || v == rt) rt = u;
  }
  if (!rt) rt = 1;
  cout << max(dp(rt, 1), dp(rt, 0)) << endl;
  return 0;
}
